package UI.menuBar;

import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.JMenu;

import controller.Menu.IMenuFileController;
import UI.actions.OpenFile;
import UI.actions.SaveFile;


public class MenuFile extends JMenu{
	/**
	 * 
	 */
	private static final long serialVersionUID = 201204301921L;
	private javax.swing.JMenuItem jMenuFileNew;
	private javax.swing.JMenuItem jMenuFileOpen;
	private javax.swing.JMenuItem jMenuFileSave;
	private javax.swing.JMenuItem jMenuFileSaveAs;
	private javax.swing.JMenuItem jMenuFilePrint;
	private javax.swing.JMenuItem jMenuFileExit;
	
	private IMenuFileController mController;
	
	public MenuFile(){
		this.setText("Archivo");

		jMenuFileNew = new javax.swing.JMenuItem();
		jMenuFileOpen = new javax.swing.JMenuItem();
        jMenuFileSave = new javax.swing.JMenuItem();
        jMenuFileSaveAs = new javax.swing.JMenuItem();
        jMenuFilePrint = new javax.swing.JMenuItem();
        jMenuFileExit = new javax.swing.JMenuItem();
        
        
        jMenuFileNew.setText("Nuevo");
        jMenuFileOpen.setText("Abrir");
        jMenuFileSave.setText("Guardar");
        jMenuFileSaveAs.setText("Guardar como");
        jMenuFilePrint.setText("Imprimir");
        jMenuFileExit.setText("Salir");

        jMenuFileNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuFileNewActionPerformed(evt);
            }
        });

        jMenuFileOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuFileOpenActionPerformed(evt);
            }

        });
        jMenuFileSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuFileSaveActionPerformed(evt);
            }
        });
        jMenuFileSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuFileSaveAsActionPerformed(evt);
            }
        });

        
        this.add(jMenuFileNew);
        this.add(jMenuFileOpen);
        this.add(jMenuFileSave);
        this.add(jMenuFileSaveAs);
        this.add(jMenuFilePrint);
        this.add(jMenuFileExit);
	}
	

	public void setController(IMenuFileController controller) {
		this.mController = controller;
	}


	private void jMenuFileNewActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		
	}
	/**
	 * Open a new XML file
	 * @param evt
	 */
	private void jMenuFileOpenActionPerformed(ActionEvent evt) {
		OpenFile of = new OpenFile();
		try{
			File f = of.getSelectedFile();
			mController.openFile(f);
		} catch (NullPointerException e){
			e.printStackTrace();
		}
	}
	
	private void jMenuFileSaveActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
	}
	
	private void jMenuFileSaveAsActionPerformed(ActionEvent evt) {
		
		SaveFile of = new SaveFile();
		try{
			File f = of.getSelectedFile();
			mController.saveFileAs(f);
		} catch (NullPointerException e){
			e.printStackTrace();
		}		
	}

}
